/**
 * 
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IActivityInvitationDao;
import com.qiyou.entity.ActivityInvitation;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 活动邀请数据操作实现
 * 
 * @author 赛艳志
 * 
 */
public class ActivityInvitationDaoImpl implements IActivityInvitationDao {
    private Connection conn = null;

    public ActivityInvitationDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(ActivityInvitation activityInvitation)
            throws SQLException {
        String sql = "insert into tb_activity_invitation(userID,acID,aiState,aiDate) values(?,?,?,?)";
        Object[] params = TransformUtil.beanTOArray(activityInvitation, false);
        return DBUtil.execute(conn, sql, params);
    }

    @Override
    public boolean doUpdate(long acID, String userID, String state)
            throws SQLException {
        String sql = "update tb_activity_invitation set aiState=? where userID=? and acID=?";
        Object[] params = { state, userID, acID };
        return DBUtil.execute(conn, sql, params);
    }

    @Override
    public boolean doDelete(long acID, String userID) throws SQLException {
        String sql = "delete from tb_activity_invitation where acID=? and userID=?";
        Object[] params = { acID, userID };
        return DBUtil.execute(conn, sql, params);
    }

    @Override
    public List<ActivityInvitation> getInvitations(long acID)
            throws SQLException {
        List<ActivityInvitation> results = null;
        String sql = "select * from tb_activity_invitation where acID=?";
        List<Object[]> all = DBUtil.query(conn, sql, acID);
        if (null != all && all.size() > 0) {
            results = new ArrayList<ActivityInvitation>(all.size());
            for (Object[] temp : all) {
                ActivityInvitation aci = (ActivityInvitation) TransformUtil.arrayToBean(temp,
                        ActivityInvitation.class);
                results.add(aci);
            }
        }
        return results;
    }

    @Override
    public List<ActivityInvitation> getInvitations(String userID)
            throws SQLException {
        List<ActivityInvitation> results = null;
        String sql = "select * from tb_activity_invitation where userID=?";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (null != all && all.size() > 0) {
            results = new ArrayList<ActivityInvitation>(all.size());
            for (Object[] temp : all) {
                ActivityInvitation aci = (ActivityInvitation) TransformUtil.arrayToBean(temp,
                        ActivityInvitation.class);
                results.add(aci);
            }
        }
        return results;
    }

    @Override
    public List<ActivityInvitation> getInvitations(Object... params)
            throws SQLException {
        List<ActivityInvitation> results = null;
        StringBuffer sql = new StringBuffer("select * from tb_activity_invitation ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (null != all && all.size() > 0) {
            results = new ArrayList<ActivityInvitation>(all.size());
            for (Object[] temp : all) {
                ActivityInvitation aci = (ActivityInvitation) TransformUtil.arrayToBean(temp,
                        ActivityInvitation.class);
                results.add(aci);
            }
        }
        return results;
    }

}
